package com.example.administrator.text;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import adapter.VideoMoitorAdapter;
import app.MyApplication;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import data.SharedPreferencesUtils;
import entity.VideoMonitor;
import util.GreenTreeNetworkUtil;
import util.HomeUtil;
import util.LogUtil;
import util.RequestDataCallback;
import util.StringUtil;
import util.Url;
import view.diaLogView.UploadDialog;

/**
 * 摄像头的页面
 */
public class VideoMonitorActivity extends AppCompatActivity {
    private List<VideoMonitor.IndoorBean> mIndoorBeen;
    private List<VideoMonitor.CommBean> mCommBeen;
    private VideoMoitorAdapter mVideoMoitorAdapter1,mVideoMoitorAdapter2;
    @Bind(R.id.frame_video_monitor)    FrameLayout mFrameLayout;
    @Bind(R.id.image_video_account)    LinearLayout mImageView;
    @Bind(R.id.recycler_video1)        RecyclerView mRVVideo1;
    @Bind(R.id.recycler_video2)        RecyclerView mRVVideo2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_monitor);
        ButterKnife.bind(this);
        HomeUtil.getHemeUtilNew().getStatusBar(this,mFrameLayout);
        init();
    }

    @OnClick(R.id.image_video_account)
    void Reture(){finish();}

    private void init() {
        LinearLayoutManager linearLayoutManagaer = new LinearLayoutManager(this);
        LinearLayoutManager linearLayoutManagaer1 = new LinearLayoutManager(this);
        linearLayoutManagaer1.setOrientation(LinearLayout.VERTICAL);
        linearLayoutManagaer.setOrientation(LinearLayout.VERTICAL);
        mRVVideo1.setLayoutManager(linearLayoutManagaer);
        mRVVideo1.setHasFixedSize(true);   //如果确定每个item的内容不会改变RecyclerView的大小，设置这个选项可以提高性能
        mRVVideo2.setLayoutManager(linearLayoutManagaer1);
        mRVVideo2.setHasFixedSize(true);
        initData();
    }

    private void initData() {
        String id = (String) SharedPreferencesUtils.getParam(this,HomeUtil.mUseridDB,"");
        String token = (String) SharedPreferencesUtils.getParam(this, HomeUtil.mTokenDB, "");
        final UploadDialog uploadDialog = new UploadDialog(this,"加载中");
        uploadDialog.show();
        HashMap<String,Object> map = new HashMap<>();
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(map); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngpcUrl_cameraUrl,stringJson,token,id,time);
        Map map1 = StringUtil.getStringUtilNew().getMapNew(this,stringJson,token,id,Url.mStirngpcUrl_cameraUrl,stirngSign,time);
        GreenTreeNetworkUtil.getInstance().doPost(this,MyApplication.getApp().getFWQString(),1,map1, new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                LogUtil.eE("",response);
                Gson gson = new Gson();
                uploadDialog.dismiss();
                VideoMonitor videoMonitor = gson.fromJson(response,VideoMonitor.class);
                if(videoMonitor.getCode() == 1){
                    mIndoorBeen = videoMonitor.getIndoor();
                    mCommBeen = videoMonitor.getComm();
                    mVideoMoitorAdapter1 = new VideoMoitorAdapter(VideoMonitorActivity.this,1,mCommBeen,null);
                    mVideoMoitorAdapter2 = new VideoMoitorAdapter(VideoMonitorActivity.this,2,null,mIndoorBeen);

                    mRVVideo1.setAdapter(mVideoMoitorAdapter2);
                    mRVVideo2.setAdapter(mVideoMoitorAdapter1);

                }else {
                    Toast.makeText(VideoMonitorActivity.this,getString(R.string.FWQ),Toast.LENGTH_SHORT).show();
                }

            }

            @Override public void onFailure(Throwable error) {
                uploadDialog.dismiss();
                StringUtil.getStringUtilNew().getExceptionCode(VideoMonitorActivity.this, error.toString());}
        });
    }
}
